package com.jimikongjian.controller;

import com.jimikongjian.models.ApplicationInfo;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.IApplicationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by fuxiuyang on 17/3/15.
 */
@RestController("applicationInfoController")
@RequestMapping("/v1")
public class ApplicationInfoController {


    @Autowired
    @Qualifier("applicationInfoService")
    private IApplicationInfoService applicationInfoService;


    /**
     * 创建一条申请信息
     * @param applicationInfo
     * @return
     */
    @RequestMapping(value = "/application_info", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage createApplicationInfo(@RequestBody ApplicationInfo applicationInfo){
        return applicationInfoService.createApplicationInfo(applicationInfo);
    }


    /**
     * 查询申请信息
     * @param applicatorId
     * @param roomCreator
     * @param status
     * @param start
     * @param length
     * @return
     */
    @RequestMapping(value = "/application_info", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectApplicationInfo(@RequestParam(name = "applicatorId", defaultValue = "-1") Integer applicatorId,
                                                 @RequestParam(name = "roomCreator", defaultValue = "-1") Integer roomCreator,
                                                 @RequestParam(name = "status", defaultValue = "-1") Integer status,
                                                 @RequestParam(name = "start", defaultValue = "0") Integer start,
                                                 @RequestParam(name = "length", defaultValue = "10") Integer length){
        if (applicatorId != null && applicatorId != -1){
            return applicationInfoService.selectApplicationInfoByApplicatorId(applicatorId, start, length);
        }
        if (status == 0){
            return applicationInfoService.selectUnreadApplicationListByRoomCreator(roomCreator, start, length);
        }
        return applicationInfoService.selectApplicationListByRoomCreator(roomCreator, start, length);
    }

    /**
     * 查询未读申请信息条数
     * @param roomCreator
     * @return
     */
    @RequestMapping(value = "/unread_application", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectApplicationInfo(@RequestParam(name = "roomCreator", defaultValue = "-1") Integer roomCreator){
        return applicationInfoService.selectUnreadCountByRoomCreator(roomCreator);
    }


    /**
     * 更新申请消息
     * @param applicationInfo
     * @return
     */
    @RequestMapping(value = "/application_info", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseMessage updateApplicationInfo(@RequestBody ApplicationInfo applicationInfo){
        return applicationInfoService.updateApplicationInfo(applicationInfo);
    }

}
